﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Collections;
using System.IO;
using System.Net;
using mshtml;



namespace OneMangaReader
{
    class Manga
    {
        #region "Attributes"

        // Manga Name
        private string _manga_id;
        
        // URL for Manga Page
        private string _url;

        // HTML of Manga Page
        private string _manga_page;

        // Short description about the manga
        private string _description;

        // Chapters of the Manga
        private Chapter[] _chapters;

        #endregion

        #region "Properties"

        public string MangaID
        {
            get { return _manga_id; }
            set { _manga_id = value; }
        }

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string MangaPage
        {
            get { return _manga_page; }
            set { _manga_page = value; }
        }

        public Chapter[] Chapters
        {
            get { return _chapters; }
            set { _chapters = value; }
        }

        #endregion

        #region "Methods"

        public Manga(string manga_id, string url)
        {
            _manga_id = manga_id;
            _url = url;
        }

        public void CreateManga()
        {
            Console.Write("retrieving html...");

            _manga_page = retrieveHTML(_url);

            Console.Write(" (ok)");
            Console.WriteLine("");
            Console.Write("extracting...");

            _chapters = extractChapters(_manga_page);

            Console.Write(" (ok)");
        }

        // Retrieve the HTML from the Manga URL
        private string retrieveHTML(string url)
        {
            // WebClient object
            WebClient client = new WebClient();

            // Retrieve resource as a stream
            Stream data = client.OpenRead(new Uri(url));

            // Retrive the text
            StreamReader reader = new StreamReader(data);
            string htmlContent = reader.ReadToEnd();

            // Cleanup
            data.Close();
            reader.Close();

            return htmlContent;
        }

        // Extract Chapters
        private Chapter[] extractChapters(string htmlContent)
        {
            // Obtain the document interface
            IHTMLDocument2 htmlDocument = (IHTMLDocument2)new mshtml.HTMLDocument();
            htmlDocument.write(htmlContent);

            IHTMLDocument2 tableContents = (IHTMLDocument2)new mshtml.HTMLDocument();

            ArrayList chapterList = new ArrayList();

            // The following is the part where the chapters are stated
            // <table class="ch-table"> ... </table>

            foreach (IHTMLElement el in
                (IHTMLElementCollection)htmlDocument.body.all)
            {

                if (el.tagName.Equals("TABLE") &&
                    el.className.Equals("ch-table"))
                {

                    tableContents.write(el.outerHTML);

                    foreach (IHTMLElement cElement in
                        (IHTMLElementCollection)tableContents.body.all)
                    {
                        if (cElement.tagName.Equals("A"))
                        {
                            string chapter_name = cElement.innerText;
                            string chapter_url = ((IHTMLAnchorElement)cElement).pathname;
                            
                            if (chapter_url.StartsWith("blank"))
                                chapter_url = chapter_url.TrimStart("blank".ToCharArray());

                            chapterList.Add(
                                new Chapter(chapter_name, chapter_url));
                        }
                    }
                }
            }

            Chapter[] chaps = new Chapter[chapterList.Count];

            for (int i = 0; i < chapterList.Count; i++)
            {
                chaps[i] = (Chapter)chapterList[i];
            }

            return chaps;
            //return (Chapter[])chapterList.ToArray();
        }


        #endregion

    }
}
